<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.Date" %>
<%@ page import="com.cwj.register.utils.DBHelper" %>

<%
    request.setCharacterEncoding("UTF-8");
    String username = request.getParameter("username");
    String password = request.getParameter("password");

//   提升变量作用域
    Connection conn = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;

    try {
//        获取连接对象，连接数据库查询
        conn = DBHelper.getConn();
        String sql = "SELECT * FROM user WHERE username = ? AND password = ?";
        pstmt = conn.prepareStatement(sql);
        pstmt.setString(1, username);
        pstmt.setString(2, password);
        rs = pstmt.executeQuery();

        if (rs.next()) {
//            1、遍历数据，是否跟数据库查询的一致
            String usernameFromDB = rs.getString("username");
            String dateFromDB = rs.getString("date");
            String genderFromDB = rs.getString("gender");

            //提升会话作用域，换窗口也可以正常使用
//            request.setAttribute("username", usernameFromDB);
//            request.setAttribute("date", dateFromDB);
//            request.setAttribute("gender", genderFromDB);
            request.getSession().setAttribute("username", usernameFromDB);
            request.getSession().setAttribute("date", dateFromDB);
            request.getSession().setAttribute("gender", genderFromDB);


//           2、 登录成功之后，下次登录的时间也要进行修改
            Date date = new Date();
//            转换成数据库对应的时间戳类
            Timestamp lastTime =new Timestamp(new Date().getTime());
            String updateSql = "UPDATE user SET date=? where username= ?";
            PreparedStatement preparedStatement = conn.prepareStatement(updateSql);
            preparedStatement.setTimestamp(1,lastTime);
            preparedStatement.setString(2,username);
            preparedStatement.executeUpdate();


//            登录成功逻辑，设置cookie，配合cookie使用。使用用户名+秘密
            String passwordFromData = rs.getString("password");
            Cookie cookie=new Cookie("user",usernameFromDB+"&"+passwordFromData);
//            两个小时
            cookie.setMaxAge(60*60*2);
            response.addCookie(cookie);

            request.getRequestDispatcher("login_success.jsp").forward(request, response);

//            response.sendRedirect("login_success.jsp");
//            3、登录失败，重新回到登录页面。并给出提示信息
        } else {
            request.setAttribute("errMsg", "用户名或密码错误，请重新登录！");
            request.getRequestDispatcher("index.jsp").forward(request, response);
        }
//        截取全部异常
    } catch (Exception e) {
//        输入参数不正确，跳转友好的提示页面
        request.getRequestDispatcher("login_error.jsp").forward(request, response);
        e.printStackTrace();
    } finally {
//       关闭资源
        try {
            if (rs != null) {
                rs.close();
            }
            if (pstmt != null) {
                pstmt.close();
            }
            if (conn != null) {
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
%>
